package com.sunday.aomi.widgets;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.sunday.common.utils.DeviceUtils;
import com.sunday.common.widgets.pickerview.adapter.WheelAdapter;
import com.sunday.common.widgets.pickerview.lib.WheelView;
import com.sunday.common.widgets.pickerview.listener.OnItemSelectedListener;
import com.sunday.aomi.R;
import com.sunday.aomi.entity.Driver;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by 刘涛 on 2017/6/7.
 */

public class SelectDriverWindow extends PopupWindow {


    @Bind(R.id.btn_cancel)
    TextView btnCancel;
    @Bind(R.id.btn_confirm)
    TextView btnConfirm;
    @Bind(R.id.wheelview)
    com.sunday.aomi.widgets.WheelView wheelview;

    private View windowView;
    private Context mContext;
    private List<Driver> dataSet = new ArrayList<>();
    private OnSelectListner onSelectListner;
    private int selectedItem;

    public SelectDriverWindow(Context context, List<Driver> datas) {
        this.mContext = context;
        this.dataSet = datas;
        windowView = LayoutInflater.from(mContext).inflate(R.layout.layout_select_driver, null);
        setContentView(windowView);
        ButterKnife.bind(this, windowView);
        int appWidth = DeviceUtils.getDisplay(mContext).widthPixels;
        this.setWidth(appWidth);
        this.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        this.setOutsideTouchable(true);
        this.setFocusable(true);
        this.setOnDismissListener(new PoponDismissListener());
        this.setBackgroundDrawable(new BitmapDrawable());
        this.setAnimationStyle(R.style.popuwindow);
        List<String> driverNames=new ArrayList<>();
        for (Driver item:dataSet){
            driverNames.add(item.getMobileName()+" "+item.getMobile());
        }
        wheelview.setOffset(1);
        wheelview.setItems(driverNames);
        wheelview.setOnWheelViewListener(new com.sunday.aomi.widgets.WheelView.OnWheelViewListener(){
            @Override
            public void onSelected(int selectedIndex, String item) {
                selectedItem=selectedIndex-1;
            }
        });
    }

    @OnClick({R.id.btn_cancel,R.id.btn_confirm})
    void onClick(View v){
        switch (v.getId()){
            case R.id.btn_cancel:
                dismiss();
                break;
            case R.id.btn_confirm:
                if (onSelectListner!=null){
                    onSelectListner.onSelect(selectedItem);
                }
                dismiss();
                break;
        }
    }




    //显示
    public void showPopupWindow(View parent) {
        if (!this.isShowing()) {
            this.showAtLocation(parent, Gravity.BOTTOM, 0, 0);
            backgroundAlpha(0.5f);
        } else {
            this.dismiss();
        }
    }

    public void setOnSelectListner(OnSelectListner onSelectListner) {
        this.onSelectListner = onSelectListner;
    }

    /**
     * 弹出的popWin关闭的事件，主要是为了将背景透明度改回来
     */
    class PoponDismissListener implements OnDismissListener {

        @Override
        public void onDismiss() {
            // TODO Auto-generated method stub
            //Log.v("List_noteTypeActivity:", "我是关闭事件");
            backgroundAlpha(1f);
            dismiss();
        }
    }



    /**
     * 设置添加屏幕的背景透明度
     */
    public void backgroundAlpha(float bgAlpha) {
        WindowManager.LayoutParams lp = ((Activity) mContext).getWindow().getAttributes();
        lp.alpha = bgAlpha; //0.0-1.0
        ((Activity) mContext).getWindow().setAttributes(lp);
    }


    public interface OnSelectListner{
        void onSelect(int p);
    }

}
